<?php


namespace app\cms\community\lib\utils\addons;


use app\cms\community\lib\utils\addons\lib\saveAddonsAbstract;
use iflow\fileSystem\lib\upLoadFile;

class uploadAddons extends saveAddonsAbstract {

    /**
     * 储存插件
     * @return bool
     * @throws exception\addonsException|exception\addonsEmptyException
     */
    public function save(): bool
    {
        $this->addonsRootPath = "{$this->addonsRootPath}{$this -> addonsConfig['addons_rule']}";
        rename($this->tempPath, $this->addonsRootPath);
        $this->removeTemp();
        return parent::save(); // TODO: Change the autogenerated stub
    }

    /**
     * 储存文件
     * @param upLoadFile $upLoadFile
     * @return $this
     */
    public function setUploadFile(upLoadFile $upLoadFile): static
    {
        $this->saveTempPath = $upLoadFile -> move('runtime/store/temp', [
            'type' => 'md5',
            // 上传插件大小不能超过5M
            'size' => 1024 * 1024 * 5
        ]);
        $this->unZip($this->saveTempPath);
        return $this;
    }

    protected function removeTemp(): static
    {
        unlink($this->saveTempPath);
        return parent::removeTemp(); // TODO: Change the autogenerated stub
    }
}
